package wat.serwerapp.usermgmt.ldap;

import wat.serwerapp.usermgmt.ldap.LdapConnector;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.DirContext;

/**
 *
 * @author P.B. & L.G. & K.K.
 */
class Role {

    static final String FILE_ATTRIBUTE_NAME = "JavaClassNames";
    static final String PARAMETER_ATTRIBUTE_NAME = "Description";
    private DirContext ctx;
    private String cn;
    private Map<String, String> localRights;

    Role(String role) throws NamingException {
        this.ctx = LdapConnector.getRolesContext();
        this.cn = role;
    }

    public Map<String, String> getRights() throws NamingException {
        if (localRights == null) {
            localRights = new HashMap<String, String>();
            Attributes att = ctx.getAttributes(cn);
            Attribute b = att.get(FILE_ATTRIBUTE_NAME);
            Attribute a = att.get(PARAMETER_ATTRIBUTE_NAME);

            if (a != null) {
                int size = a.size();
                for (int i = 0; i < size; i++) {
                    String file = (String) a.get(i);
                    String parameter = (String) b.get(i);
//                System.out.println(a.get(i));
//                System.out.println(b.get(i));
                    localRights.put(file, parameter);
                }
            }
        }
        return localRights;
    }

    public void setRights(Map<String, String> rights) throws NamingException, Exception {
        Attributes att = ctx.getAttributes(cn);
        Attribute b = att.get(FILE_ATTRIBUTE_NAME);
        Attribute a = att.get(PARAMETER_ATTRIBUTE_NAME);
        if(a==null) {
            a = new BasicAttribute(PARAMETER_ATTRIBUTE_NAME);
            b = new BasicAttribute(FILE_ATTRIBUTE_NAME);
            att.put(a);
            att.put(b);
        }

        Set<String> keys = rights.keySet();

        a.clear();
        b.clear();
        getRights().clear();

        for (String rightName : keys) {
            String rightFile = rights.get(rightName);
//            if (getRights().containsKey(rightName)) {
//                throw (new Exception("Rola posiada juz to uprawnienie: " + rightName));
//            }
            a.add(rightName);
            b.add(rightFile);
            getRights().put(rightName, rightFile);
        }
        ctx.modifyAttributes(cn, ctx.REPLACE_ATTRIBUTE, att);
    }

    public void addRight(String rightName, String rightFile) throws NamingException, Exception {
        if (getRights().containsKey(rightName)) {
            throw (new Exception("Rola posiada juz to uprawnienie: " + rightName));
        }
        Attributes att = ctx.getAttributes(cn);
        Attribute b = att.get(FILE_ATTRIBUTE_NAME);
        Attribute a = att.get(PARAMETER_ATTRIBUTE_NAME);

        a.add(rightName);
        b.add(rightFile);
        getRights().put(rightName, rightFile);
        ctx.modifyAttributes(cn, ctx.REPLACE_ATTRIBUTE, att);
    }

    public void removeRight(String rightName) throws NamingException, Exception {
        if (!getRights().containsKey(rightName)) {
            throw (new Exception("Rola nie posiada tego uprawnienia: " + rightName));
        }
        Attributes att = ctx.getAttributes(cn);
        Attribute b = att.get(FILE_ATTRIBUTE_NAME);
        Attribute a = att.get(PARAMETER_ATTRIBUTE_NAME);

        String rightFile = getRights().get(rightName);
        a.remove(rightName);
        b.remove(rightFile);
        getRights().remove(rightName);
        ctx.modifyAttributes(cn, ctx.REPLACE_ATTRIBUTE, att);
    }
}
